import pymysql
from lxml import etree
import requests
import time


class DB:
    def __init__(self, host='localhost', port=3306, db='', user='root', password='root', charset='utf8'):
        self.conn = pymysql.connect(host=host, port=port, database=db, user=user,password=password, charset=charset)
        self.cur = self.conn.cursor()

    def __enter__(self):
        return self.cur

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.conn.commit()
        self.cur.close()
        self.conn.close()

def dataUrl(url,enc):

    header = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'}
    res=requests.get(url,headers=header)
    res.encoding=enc
    html = etree.HTML(res.text)
    return html

def dataHtml(html):

    contents = html.xpath('//*[@class="ABox"]')
    for i in contents:
        hrefinfo=i.xpath('./a/@href')[0]
        nameinfo=i.xpath('./a/img/@alt')[0]
        adressinfo=i.xpath('./a/img/@src')[0]
        print(hrefinfo,nameinfo,adressinfo)
        para=[hrefinfo,nameinfo,adressinfo]
        # print(para)
        # sql = 'insert into xpath(href_,name_, adress_) values(%s,%s,%s)'
        db.execute(
            'insert into xpath(href_,name_, adress_) values (%s,%s,%s)',para)
def main():
    for i in range(1,2):
        url='https://www.mmonly.cc/gqbz/list_41_{}.html'.format(i)
        enc='gb2312'
        html=dataUrl(url,enc)
        dataHtml(html)
        time.sleep(1)
if __name__ == '__main__':
    with DB(host='localhost',port=3306,user='usert',password='123456',db='guojiuchang') as db:
        main(db)
if __name__ == '__main__':
    with DB(db='admin' ) as db:
        main()
